# -*- coding: utf-8 -*-

import hashlib
from base import UserCase, Runner, TestSuite


class Case(UserCase):

    def test_action_log_list(self):
        p = {
            'name': self.randhan(8),
            'username': self.randchar(8),
            'password': hashlib.md5(self.randchar(8)).hexdigest(),
        }
        res = self.c.open('/api/corp/create', p)
        self.assertEqual(res['code'], 0)

        res = self.c.open('/api/action-log/list', {'orderBy': 'action_log.id', 'orderDesc': '1'}, 'GET')
        self.assertEqual(res['code'], 0)
        self.assertEqual(res['data']['itemList'][0]['type'], '/api/corp/create')


    def test_action_log_list_filter_by_corp(self):
        p = {
            'name': self.randhan(8),
            'username': self.randchar(8),
            'password': hashlib.md5(self.randchar(8)).hexdigest(),
        }
        res = self.c.open('/api/corp/create', p)
        self.assertEqual(res['code'], 0)

        p = {
            'name': self.randhan(8),
            'username': self.randchar(8),
            'password': hashlib.md5(self.randchar(8)).hexdigest(),
        }
        res = self.c.open('/api/corp/create', p)
        self.assertEqual(res['code'], 0)
        corp = res['data']['id']

        res = self.c.open('/api/action-log/list', {'orderBy': 'action_log.id', 'orderDesc': '1'}, 'GET')
        self.assertEqual(res['code'], 0)
        self.assertEqual(res['data']['count'] >= 2, True)


        res = self.c.open('/login', {'username': p['username'], 'password': p['password']})
        self.assertEqual(res['code'], 0)

        p = {
            'name': self.randhan(8),
        }
        res = self.c.open('/api/my-corp/update', {'name': p['name']})
        self.assertEqual(res['code'], 0)

        self.login()

        res = self.c.open('/api/action-log/list', {'orderBy': 'action_log.id', 'orderDesc': '1'}, 'GET')
        self.assertEqual(res['code'], 0)
        self.assertEqual(res['data']['count'] >= 2, True)

        res = self.c.open('/api/action-log/list', {'orderBy': 'action_log.id', 'orderDesc': '1', 'corp': corp}, 'GET')
        self.assertEqual(res['code'], 0)
        self.assertEqual(res['data']['count'], 2)



test_list = [
    'test_action_log_list',
    'test_action_log_list_filter_by_corp',
]
Suite = TestSuite([Case(t) for t in test_list])


if __name__ == '__main__':
    Runner.run(Suite)



 
